local chougui = fk.CreateSkill{
  name = "kl__chougui",
}

Fk:addPoxiMethod{
  name = "kl__chougui",
  prompt = "#kl__chougui-give",
  card_filter = function(to_select, selected, data, extra_data)
    if #selected <= 2 then
      if #selected == 0 then
        return true
      elseif #selected == 1 then
        if table.contains(data[1][2], selected[1]) then
          return not table.contains(data[1][2], to_select)
        elseif table.contains(data[2][2], selected[1]) then
          return not table.contains(data[2][2], to_select)
        elseif table.contains(data[3][2], selected[1]) then
          return not table.contains(data[3][2], to_select)
        end
      elseif #selected == 2 then
        if table.contains(data[1][2], selected[1]) and table.contains(data[2][2], selected[2]) then
          return table.contains(data[3][2], to_select)
        elseif table.contains(data[1][2], selected[1]) and table.contains(data[3][2], selected[2]) then
          return table.contains(data[2][2], to_select)
        elseif table.contains(data[2][2], selected[1]) and table.contains(data[3][2], selected[2]) then
          return table.contains(data[1][2], to_select)
        end
        if table.contains(data[1][2], selected[2]) and table.contains(data[2][2], selected[1]) then
          return table.contains(data[3][2], to_select)
        elseif table.contains(data[1][2], selected[2]) and table.contains(data[3][2], selected[1]) then
          return table.contains(data[2][2], to_select)
        elseif table.contains(data[2][2], selected[2]) and table.contains(data[3][2], selected[1]) then
          return table.contains(data[1][2], to_select)
        end
      end
    end
  end,
  feasible = function(selected)
    return #selected > 0
  end,
}

local spec = {
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, chougui.name)
    if #room:getOtherPlayers(player, false) == 0 then return end
    local cards = player:getCardIds("hej")
    if #cards > 0 then
      local hand = player:getCardIds("h")
      local equip = player:getCardIds("e")
      local judge = player:getCardIds("j")
      local card = player.room:askToPoxi(player, {
        poxi_type = "kl__chougui",
        data = {
          { "kl__judge", judge },
          { "kl__equip", equip },
          { "kl__hand", hand },
        },
        cancelable = true,
      })
      if #card > 0 then
        local to = room:askToChoosePlayers(player, {
          targets = room:getOtherPlayers(player, false),
          min_num = 1,
          max_num = 1,
          skill_name = chougui.name,
          prompt = "#kl__chougui-choose",
          cancelable = false,
        })[1]
        room:moveCardTo(card, Player.Hand, to, fk.ReasonGive, chougui.name, nil, false, player)
      end
    end
  end
}

chougui:addEffect(fk.AfterCardsMove, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(chougui.name) and player:usedSkillTimes(chougui.name, Player.HistoryTurn) < 3 and
      player.phase ~= Player.Draw then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Player.Hand then
          return move.skillName ~= chougui.name
        end
      end
    end
  end,
  on_use = spec.on_use,
})

chougui:addEffect(fk.DamageInflicted, {
  anim_type = "masochism",
  trigger_times = function(self, event, target, player, data)
    return data.damage
  end,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(chougui.name) and player:usedSkillTimes(chougui.name, Player.HistoryTurn) < 3
  end,
  on_use = spec.on_use,
})


return chougui
